<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Model\Photo;
use App\Model\Album;

use DB;
use Auth;

use Cart;


class MVCController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


    public function getList()
    {
       // $res = Photo::all();
        //dd($res);
        //
        //$res = Photo::find(42);
        //
        //$res = Photo::select('id','name')->find(42);
        
        //$res = Photo::where('id','>',20)->select('id','name')->get();
        //$count  = Photo::count();
        //$max = Photo::max('id');

        //dd($max); 
        //关联查询
        
    }


    public function getAdd()
    {
        //入库方式一  必须指明 可以填充的数据 create
       
    //protected $fillable=['intro','intro'];
       /* $data = ['name'=>'add','intro'=>'model add'];
        $res = Photo::create($data);*/
       
        //入库方式二  save()
        $photo = new Photo();
        $photo->name='newphpto59';
        $photo->intro='intro';
        $res = $photo->save();
        dd($res);
    }


    public function getUpdate($id)
    {
        $photo = Photo::find($id);
        $photo->name='update';

        $res = $photo->save();
        dd($res);

    }


    public function getDel($id)
    {
        //$res = Photo::where('id',$id)->delete();
        $photo = Photo::find($id);
        $photo && ($res=$photo->delete());
        /*if($photo){
            $res = $photo->delete();
            dd($res);
        }*/
    }


    public function getJoin()
    {
        $res = DB::table('albums')
                  ->leftjoin('photos','albums.id','=','photos.album_id')
                  ->select('photos.id as pid','photos.name as pname','albums.id','albums.name')
                  ->where('photos.id','>',40)
                  ->orderBy('photos.id','desc')
                  ->get();
        foreach ($res as  $val) {
           print_r($val);
        }
    }


    public function getHasmany()
    {
        //$album = Album::find(9);
        //dd($album);
        $photos = Album::find(6)->photo;
        foreach ($photos as $val) {
            echo "<pre>";
           print_r($val);
        }
    }


    public function getUser()
    {
        //dd(Auth::check());
        dd(Auth::user());
       // Auth::logout();
        //
        //
        //$email="281008102@qq.com";
        $password='123456';
        $mobile = '12345678909';
        $res=Auth::attempt(['mobile' => $mobile, 'password' => $password]);
        var_dump($res);
        dd(Auth::user());
    }

    /**
     * 购物车demo
     */
    public function getCart()
    {

        /*Cart::destroy();
        Cart::add('1', '风景1', 1, 10.0);
        //Cart::add('2', '风景2', 1, 10.0);
      //  $all =  Cart::content();


        print_r(Cart::tax());
        echo "------------";
        print_r(Cart::count());

        echo "------------";
        print_r(Cart::total());
        echo "------------";
        print_r(Cart::subtotal());
      /*  foreach($all as $item){
            //Cart::update( $item->rowId, ['name' => '179']);
            Cart::remove($item->rowId);
        }

        dd(Cart::content());*/
        //添加商品到购物车   id, name, quantity, price  opt
    /*    Cart::add('1', '风景1', 2, 9.99,['size'=>'xxl']);

        // Array form 数组表单
        Cart::add(array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => array('size' => 'l')));
        */
        /*$malt = Cart::add(array(
            array('id' => '1', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00),
            array('id' => '2', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => array('size' => 'large'))
        ));
       $all =  Cart::content();
        foreach($all as $item){
            echo "<pre/>";
            print_r($item);
        }*/



    }
}
